package com.dataimport.service.impl;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.sourceentity.StudentCourseInfo;
import com.dataimport.entity.sourceentity.StudentCurriculumInformation;
import com.dataimport.entity.targetentity.Course;
import com.dataimport.entity.targetentity.Curriculum;
import com.dataimport.mapper.CurriculumMapper;
import com.dataimport.mapper.SchoolCalendarMapper;
import com.dataimport.mapper.StudentCurriculumInformationMapper;
import com.dataimport.service.CurriculumService;
import com.dataimport.utils.HasNullFieldUtil;
import com.dataimport.utils.SchoolCalendarCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CurriculumServiceImpl extends ServiceImpl<CurriculumMapper, Curriculum> implements CurriculumService {
    @Autowired
    private StudentCurriculumInformationMapper curriculumInformationMapper;
    @Autowired
    private SchoolCalendarMapper schoolCalendarMapper;
    @Override
    public void dealWithCurriculum() {
        DynamicDataSourceContextHolder.push("mysql2");
        List<StudentCurriculumInformation> curriculumInformations = curriculumInformationMapper.selectList(null);
        if(CollectionUtils.isNotEmpty(curriculumInformations)){
            List<Curriculum> studentCurriculumInformations = curriculumInformations.stream().parallel().map(studentCurriculumInformation-> {
                Curriculum curriculum = new Curriculum();
                int  year = Integer.parseInt(studentCurriculumInformation.getXn())+1;
                String years = studentCurriculumInformation.getXn()+"-"+year+"-"+studentCurriculumInformation.getXq();
                int calendar =  SchoolCalendarCache.selectSchoolCalendar(schoolCalendarMapper).get(years);
                curriculum.setCalendarId(Long.valueOf(calendar));
                curriculum.setCourseId(studentCurriculumInformation.getKcbm());
                curriculum.setTeacherId(studentCurriculumInformation.getDyrkjs());
                curriculum.setTeachClass(studentCurriculumInformation.getJxbh());
                return curriculum;
            }).collect(Collectors.toList());
            studentCurriculumInformations.forEach(System.out::println);
            DynamicDataSourceContextHolder.poll();
            insertCurriculum(studentCurriculumInformations);
        }
    }
    @Transactional(rollbackFor = Exception.class)
    void insertCurriculum(List<Curriculum> studentCurriculumInformations){
        DynamicDataSourceContextHolder.push("mysql1");
        this.saveBatch(studentCurriculumInformations);
    }
}
